//
//  SearchDetailViewController.swift
//  OverseasOnlineShopping
//
//  Created by 唐先才 on 16/3/10.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import UIKit

class SearchDetailViewController: BaseCollectionViewController {
    
    var page: Int = 0
    var isPulling: Bool = false
    var catUrl: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = BACKGROUD_COLOR
        self.page = 1
        createData()
        createRefresh()
    }
    
    func createData() {
        if isPulling {
            self.baseDataArray.removeAllObjects()
        }
        
        let manager = AFHTTPRequestOperationManager()
        guard let catUrl = self.catUrl else {
            return
        }
        let parameters = ["brightUrl": "",
            "catUrl": catUrl,
            "exceptMallUrl": "",
            "homeShow": "0",
            "infoNation": "0",
            "infoType": "100",
            "isTop": "1",
            "keyWord": "",
            "mallUrl": "",
            "pageIndex": self.page,
            "pageSize": "20",
            "tagUrl": "",
        ]
        manager.POST(searchDetail_url, parameters: parameters, success: { (operation: AFHTTPRequestOperation!, responsObject: AnyObject!) -> Void in
            guard let array = responsObject as? [[String: AnyObject]] else {
                print("转返回数据失败")
                return
            }
            for dict in array {
                let model = TxcModel(dict: dict)
                self.baseDataArray.addObject(model)
            }
            guard let collectionView = self.baseCollectionView else {
                print("失败么")
                return
            }
            collectionView.reloadData()
            }) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
            SVProgressHUD.showErrorWithStatus(AppNetErrorTips)
        }
    }
    
    func createRefresh() {
        guard let collectionView = self.baseCollectionView else {
            print("")
            return
        }
        
        collectionView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: "downRefresh")
        collectionView.mj_footer = MJRefreshAutoFooter(refreshingTarget: self, refreshingAction: "upRefresh")
    }
    
    func downRefresh() {
        guard let collectionView = self.baseCollectionView else {
            print("")
            return
        }
        isPulling = true
        self.page = 1
        collectionView.mj_header.beginRefreshing()
        self.createData()
        collectionView.mj_header.endRefreshing()
    }
    
    func upRefresh() {
        guard let collectionView = self.baseCollectionView else {
            print("")
            return
        }
        isPulling = false
        self.page++
        collectionView.mj_footer.beginRefreshing()
        self.createData()
        collectionView.mj_footer.endRefreshing()
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

